package com.xinhe.cn.cpbox.task;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateUtil;
import com.xinhe.cn.cpbox.mapper.UserOrderMapper;
import com.xinhe.cn.cpbox.service.OrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

/**
 * @author HB
 * @Date 2021/10/11 17:22
 */
@Slf4j
@Component
@EnableScheduling
@EnableAsync
public class CpBoxTask {
    @Autowired
    UserOrderMapper userOrderMapper;
    @Autowired
    OrderService orderService;

    /**
     * 定时任务
     */
    @Async
    @Scheduled(cron = "0 05 12 * * ?")
    public void clearAndSummaryOrder() {
        // 开始日期
        String startTime = DateUtil.parse(DateUtil.today()).offset(DateField.DAY_OF_YEAR, -1).toString();
        // 结束日期
        String endTime = DateUtil.parse(DateUtil.today()).toString();

        // 清空未支付无用订单
        orderService.clearNoPayOrder(endTime);
        // 计算提成，并生成用户记录
        orderService.summaryMoney(startTime, endTime);
    }
}
